# Copyright 2023 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

package(default_visibility = ["//visibility:public"])

cc_library(
    name = "datasource_adaptor",
    srcs = ["datasource_adaptor.cc"],
    hdrs = ["datasource_adaptor.h"],
    deps = [
        "//api:core_cc_proto",
        "//engine/core:arrow_helper",
        "//engine/core:tensor",
        "//engine/core:tensor_constructor",
        "@com_github_gabime_spdlog//:spdlog",
    ],
)

cc_library(
    name = "arrow_sql_adaptor",
    srcs = ["arrow_sql_adaptor.cc"],
    hdrs = ["arrow_sql_adaptor.h"],
    deps = [
        ":datasource_adaptor",
        ":datasource_cc_proto",
        "//engine/core:tensor_constructor",
        "//engine/util:spu_io",
        "@org_apache_arrow//:arrow",
        "@org_apache_arrow//:arrow_flight",
    ],
)

cc_binary(
    name = "arrow_sql_adaptor_test",
    srcs = ["arrow_sql_adaptor_test.cc"],
    deps = [
        ":arrow_sql_adaptor",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@com_google_googletest//:gtest",
    ],
)

cc_library(
    name = "odbc_connector",
    srcs = ["odbc_connector.cc"],
    hdrs = ["odbc_connector.h"],
    deps = [
        "@org_pocoproject_poco//:poco",
        "@yacl//yacl/base:exception",
    ],
)

cc_library(
    name = "odbc_adaptor",
    srcs = ["odbc_adaptor.cc"],
    hdrs = ["odbc_adaptor.h"],
    deps = [
        ":datasource_adaptor",
        ":datasource_cc_proto",
        ":odbc_connector",
        "//engine/core:primitive_builder",
        "//engine/core:string_tensor_builder",
        "//engine/util:spu_io",
        "@org_pocoproject_poco//:poco",
        "@yacl//yacl/base:exception",
    ],
)

cc_test(
    name = "odbc_adaptor_sqlite_test",
    srcs = ["odbc_adaptor_sqlite_test.cc"],
    deps = [
        ":odbc_adaptor",
        "@com_google_googletest//:gtest_main",
    ],
)

# binary for integration test
# run odbc_adaptor_mysql_test with `/engine/datasource/run_odbc_adaptor_mysql_test.sh`
cc_binary(
    name = "odbc_adaptor_mysql_test",
    srcs = ["odbc_adaptor_mysql_test.cc"],
    deps = [
        ":odbc_adaptor",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@com_google_googletest//:gtest",
    ],
)

cc_library(
    name = "router",
    hdrs = ["router.h"],
    deps = [":datasource_cc_proto"],
)

proto_library(
    name = "embed_router_proto",
    srcs = ["embed_router.proto"],
    deps = [":datasource_proto"],
)

cc_proto_library(
    name = "embed_router_cc_proto",
    deps = [":embed_router_proto"],
)

proto_library(
    name = "datasource_proto",
    srcs = ["datasource.proto"],
)

cc_proto_library(
    name = "datasource_cc_proto",
    deps = [":datasource_proto"],
)

cc_library(
    name = "embed_router",
    srcs = ["embed_router.cc"],
    hdrs = ["embed_router.h"],
    deps = [
        ":embed_router_cc_proto",
        ":router",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/strings",
        "@yacl//yacl/base:exception",
    ],
)

cc_test(
    name = "embed_router_test",
    srcs = ["embed_router_test.cc"],
    deps = [
        ":embed_router",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "datasource_adaptor_mgr",
    srcs = ["datasource_adaptor_mgr.cc"],
    hdrs = ["datasource_adaptor_mgr.h"],
    deps = [
        ":arrow_sql_adaptor_factory",
        ":csvdb_adaptor_factory",
        ":datasource_adaptor",
        ":datasource_cc_proto",
        ":dp_adaptor_factory",
        ":odbc_adaptor_factory",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/synchronization",
    ],
)

cc_library(
    name = "datasource_adaptor_factory",
    hdrs = ["datasource_adaptor_factory.h"],
    deps = [
        ":datasource_adaptor",
        ":datasource_cc_proto",
    ],
)

cc_library(
    name = "arrow_sql_adaptor_factory",
    hdrs = ["arrow_sql_adaptor_factory.h"],
    deps = [
        ":arrow_sql_adaptor",
        ":datasource_adaptor_factory",
    ],
)

cc_library(
    name = "odbc_adaptor_factory",
    srcs = ["odbc_adaptor_factory.cc"],
    hdrs = ["odbc_adaptor_factory.h"],
    deps = [
        ":datasource_adaptor_factory",
        ":odbc_adaptor",
    ],
)

proto_library(
    name = "csvdb_conf_proto",
    srcs = ["csvdb_conf.proto"],
)

cc_proto_library(
    name = "csvdb_conf_cc_proto",
    deps = [":csvdb_conf_proto"],
)

cc_library(
    name = "duckdb_wrapper",
    srcs = ["duckdb_wrapper.cc"],
    hdrs = ["duckdb_wrapper.h"],
    deps = [
        ":csvdb_conf_cc_proto",
        "//api/v1:column_cpp_proto",
        "//engine/util:filepath_helper",
        "@com_github_duckdb//:duckdb",
        "@com_github_gflags_gflags//:gflags",
        "@com_google_absl//absl/strings",
        "@yacl//yacl/base:exception",
    ],
)

cc_test(
    name = "duckdb_wrapper_test",
    srcs = ["duckdb_wrapper_test.cc"],
    deps = [
        ":duckdb_wrapper",
        "@com_github_brpc_brpc//:butil",
        "@com_github_fmtlib_fmt//:fmtlib",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "csvdb_adaptor",
    srcs = ["csvdb_adaptor.cc"],
    hdrs = ["csvdb_adaptor.h"],
    deps = [
        ":datasource_adaptor",
        ":datasource_cc_proto",
        ":duckdb_wrapper",
        "//api/v1:column_cpp_proto",
        "//engine/core:tensor_constructor",
        "//engine/util:spu_io",
        "//engine/util:tensor_util",
        "@com_google_absl//absl/strings",
        "@yacl//yacl/base:exception",
    ],
)

cc_library(
    name = "dp_adaptor",
    srcs = ["dp_adaptor.cc"],
    hdrs = ["dp_adaptor.h"],
    deps = [
        ":datasource_adaptor",
        ":datasource_cc_proto",
        ":duckdb_wrapper",
        "//api/v1:column_cpp_proto",
        "//engine/core:tensor_constructor",
        "//engine/exe:flags",
        "//engine/util:spu_io",
        "//engine/util:tensor_util",
        "@com_google_absl//absl/strings",
        "@dataproxy//dataproxy_sdk/cc:data_proxy_stream",
        "@yacl//yacl/base:exception",
    ],
)

cc_binary(
    name = "dp_adaptor_kuscia_test",
    srcs = ["dp_adaptor_kuscia_test.cc"],
    deps = [
        ":dp_adaptor",
        "@com_google_absl//absl/debugging:failure_signal_handler",
        "@com_google_absl//absl/debugging:symbolize",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@com_google_googletest//:gtest",
    ],
)

cc_test(
    name = "dp_adaptor_test",
    srcs = ["dp_adaptor_test.cc"],
    deps = [
        ":dp_adaptor",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "dp_adaptor_factory",
    srcs = ["dp_adaptor_factory.cc"],
    hdrs = ["dp_adaptor_factory.h"],
    deps = [
        ":datasource_adaptor_factory",
        ":dp_adaptor",
    ],
)

cc_test(
    name = "csvdb_adaptor_test",
    srcs = ["csvdb_adaptor_test.cc"],
    deps = [
        ":csvdb_adaptor",
        "//engine/core:tensor_constructor",
        "@com_github_brpc_brpc//:butil",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "csvdb_adaptor_factory",
    srcs = ["csvdb_adaptor_factory.cc"],
    hdrs = ["csvdb_adaptor_factory.h"],
    deps = [
        ":csvdb_adaptor",
        ":datasource_adaptor_factory",
    ],
)

proto_library(
    name = "http_router_proto",
    srcs = ["http_router.proto"],
    deps = [
        ":datasource_proto",
        "//api:common_proto",
        "//api:status_proto",
    ],
)

cc_proto_library(
    name = "http_router_cc_proto",
    deps = [
        ":http_router_proto",
    ],
)

cc_library(
    name = "http_router",
    srcs = ["http_router.cc"],
    hdrs = ["http_router.h"],
    deps = [
        ":http_router_cc_proto",
        ":router",
        "@com_github_brpc_brpc//:brpc",
        "@com_google_absl//absl/strings",
        "@yacl//yacl/base:exception",
    ],
)

cc_library(
    name = "kuscia_datamesh_router",
    srcs = ["kuscia_datamesh_router.cc"],
    hdrs = ["kuscia_datamesh_router.h"],
    deps = [
        ":csvdb_conf_cc_proto",
        ":router",
        "//engine/util:filepath_helper",
        "@com_github_brpc_brpc//:bthread",
        "@com_github_brpc_brpc//:butil",
        "@com_github_grpc_grpc//:grpc++",
        "@com_google_absl//absl/strings",
        "@kuscia//proto/api/v1alpha1/datamesh:domaindata_cpp_grpc",
        "@kuscia//proto/api/v1alpha1/datamesh:domaindatasource_cpp_grpc",
        "@yacl//yacl/base:exception",
    ],
)

cc_binary(
    name = "kuscia_datamesh_router_test",
    srcs = [
        "kuscia_datamesh_router_test.cc",
    ],
    deps = [
        ":kuscia_datamesh_router",
        "@com_google_absl//absl/debugging:failure_signal_handler",
        "@com_google_absl//absl/debugging:symbolize",
    ],
)
